/**
 *
 * @properties={typeid:24,uuid:"4de02e28-676c-4f9b-a7dc-23bf8a0898d2"}
 */
function alta()
{
	if ((verif_sarta(origen,rubro,subrubro,cuenta,subcuenta) == 1) && (verif_alta() == 1))
	{
		databaseManager.saveData()
		databaseManager.setAutoSave(true)
	}
	else
	{
		globals.gDialog('Error','No pudo dar de alta','OK','CerrarForm')
		databaseManager.rollbackEditedRecords()
	}
}

/**
 *
 * @properties={typeid:24,uuid:"9442fa1f-a5f9-4af9-a33a-e0632186b89f"}
 */
function existe()
{
	var orig = arguments[0]
	var rub = arguments[1]
	var subr = arguments[2]
	var cta = arguments[3]
	var subcta = arguments[4]
	                       
	var fs = databaseManager.getFoundSet('infocom','plan_de_cuentas')
	fs.loadAllRecords()
	
	fs.find()
	fs.origen = orig
	fs.rubro = rub
	fs.subrubro = subr
	fs.cuenta = cta
	fs.subcuenta = subcta
	var s = fs.search()
	
	if (s == 1)
	{
		if ((cta != 0) && (scta != 0))
			return !fs.marca_terminal
		return true
	}
	return false
}

/**
 *
 * @properties={typeid:24,uuid:"ded46c74-5c25-45cd-afc3-472064ee80bd"}
 */
function off()
{
	databaseManager.setAutoSave(false)
	controller.newRecord()
}

/**
 *
 * @properties={typeid:24,uuid:"ea659b8a-0e53-4488-838c-a90992099345"}
 */
function verif_alta()
{
	var res = 0
	if (rubro != 0)
	{
		if (existe (origen,0,0,0,0))
			if (subrubro != 0)
			{
				if (existe (origen,rubro,0,0,0))
					if (cuenta != 0)
					{
						if (existe (origen,rubro,subrubro,0,0))
							if (subcuenta != 0)
							{
								if (existe (origen,rubro,subrubro,cuenta,0))
									if (existe (origen,rubro,subrubro,cuenta,subcuenta))
										return 1
							}
							else
								res = 1
					}
					else
						res = 1
			}
			else
				res = 1
	}
	else
		res = 1
	return !res
}

/**
 * PARAM: origen,rubro,subrubro,cuenta,subcuenta
 *
 * @properties={typeid:24,uuid:"ae71a141-17b7-407a-8a8c-3819cd1b8251"}
 */
function verif_sarta()
{
	// PARAM: origen,rubro,subrubro,cuenta,subcuenta
	var res = 1
	var i = 4
	while ((i >= 0) && (arguments[i] == 0))
		i--
	
	while (i >= 0)
	{
		if (arguments[i] == 0)
			res = 0
		i--
	}
	return res
}
